package cn.hn.tx.rebot.poll.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hn.comm.LocalFiles;
import cn.hn.comm.Tools;
import cn.hn.net.Kit;
import cn.hn.tx.rebot.poll.model.RebotPoll;
import cn.hn.tx.rebot.poll.model.RebotPollOption;

public class CrawlPollServie_Xeixe_Impl extends CrawlPollServie_DiscuzX2_Impl {

	public static void main(String[] args) {
		CrawlPollServie_Xeixe_Impl cxi = new CrawlPollServie_Xeixe_Impl();
		cxi.getRebotPoll(null);
	}

	public RebotPoll getRebotPoll(String url) {

		System.out.println("抓取投票：" + url);

		try {
			Kit kit = new Kit();
			// 得到日志HTML
			LocalFiles lf = new LocalFiles();
			String html = lf.readTxtFile("D:\\node.txt");
			// String html = Kit.httpRequest(url, null, "GET", "gb2312");
			if (html != null) {
				// 得到标题
				List<String> htmlTitle = Tools.getHtmlNodes(html,
						"id=\"thread_subject\">(.*?)</a>");
				String subject = htmlTitle.get(0).trim();

				// for(String options:htmlOptions)
				// {
				// System.out.println(options);
				// }
				// if(true)
				// {
				// return null;
				// }

				// 数据库中找一下有没有一样的

				List listSubject = this.getRebotPollService()
						.queryListRebotPollBySubject(subject);
				if (listSubject.size() == 0) {
					// 得到正文
					// List<String> htmlContent = Tools.getHtmlNodes(html,
					// "<div class=.cont f14.*?>(.*?)</div>");
					// String content = htmlContent.get(0).trim();
					// 得到选项
					List<String> htmlOptions = Tools
							.getHtmlNodes(html,
									"<label for=\"option_[0-9]\">[0-9]. &nbsp;(.*?)</label>");

					if (htmlOptions.size() > 0) {
						RebotPoll poll = new RebotPoll();
						poll.setUrl(url);
						List<RebotPollOption> listRebotPollOption = new ArrayList<RebotPollOption>();
						for (String strOption : htmlOptions) {
							RebotPollOption rpo = new RebotPollOption();
							rpo.setOption(strOption);
							listRebotPollOption.add(rpo);
						}
						poll.setOptions(listRebotPollOption);
						return poll;
					}

					return null;
				} else {
					return null;
				}

			} else {
				return null;
			}
		} catch (Exception e) {
			// e.printStackTrace();
			this.getExceptionService().insertException(
					this.getClass().getName(), "getRebotPoll()", e.toString());
			return null;
		}

	}

	public List<RebotPoll> getRebotPolls(String url) {

		// 日志队列
		List<RebotPoll> listRebotPoll = new ArrayList<RebotPoll>();

		List<String> listRebotPollUrl = new ArrayList<String>();

		String html = null;

		LocalFiles lf = new LocalFiles();
		// html=lf.readTxtFile("D:\\html.txt");

		// kit.httpRequest("http://61.135.169.105", null, "GET","gb2312");

		html = Kit.httpRequest(url, null, "GET", "GBK");
		if (html != null) {

			// 得到所有日志节点
			List<String> htmlNodes = Tools.getHtmlNodes(html,
					"<h4 class=\"h\">(.*?)</h4>");

			// 得到所有日志节点的超链接地址
			for (String node : htmlNodes) {
				// System.out.println("node="+node);
				// 链接地址
				List<String> htmlANodes = Tools.getHtmlNodes(node,
						"<a.href=\"(.{1,30}html)\".*?target=.*?>");
				listRebotPollUrl.add(getUrl_Prefix() + htmlANodes.get(0));
			}

			for (String rebotPollUrl : listRebotPollUrl) {

				RebotPoll rebotPoll = getRebotPoll(rebotPollUrl);
				if (rebotPoll != null) {
					listRebotPoll.add(rebotPoll);
				}
			}
			return listRebotPoll;
		} else {
			return null;
		}
	}

}
